

package com.hexinfo.dmpro.dev.data.transfer.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hexinfo.dmpro.common.annotation.AnonymousAccess;
import com.hexinfo.dmpro.dev.data.transfer.model.DevDataExportTask;
import com.hexinfo.dmpro.dev.data.transfer.service.IDevDataExportTaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotNull;
import java.util.List;


/**
 * 总导出任务表
 *
 * @author yyh.huang
 * @date 2023-10-25 19:16:06
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/api/dev/data/export/task")
@Api(value = "devDataExportTask", tags = "开发数据导出表管理")
public class DevDataExportTaskController {

    private final IDevDataExportTaskService devDataExportTaskService;

    /**
     * 分页查询
     *
     * @param page              分页对象
     * @param devDataExportTask 总导出任务表
     *                          WrapperEnhancer.like(devDataExportTask); //自动拼接存在值sql的方法
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
    public ResponseEntity<Object> getDevDataExportTaskPage(Page page, DevDataExportTask devDataExportTask) {
        return ResponseEntity.ok(devDataExportTaskService.queryList(page, devDataExportTask));
    }

    @ApiOperation(value = "开发数据导出任务刷新", notes = "开发数据导出任务刷新")
    @PutMapping("/reset")
    public ResponseEntity<Object> reset(@RequestBody @NotNull List<String> ids) {
        log.info("开发数据导出任务刷新：{}", StrUtil.join(",", ids));
        devDataExportTaskService.reset(ids);
        return ResponseEntity.ok("ok");
    }

    @ApiOperation(value = "统计FTP文件占用", notes = "统计FTP文件占用")
    @GetMapping("/countFtpTakeUp")
    public ResponseEntity<Object> countFtpTakeUp() {
        log.info("统计FTP文件占用");
        return ResponseEntity.ok(devDataExportTaskService.countFtpTakeUp());
    }

    /**
     * 通过id查询总导出任务表
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}")
    public ResponseEntity<Object> getById(@PathVariable("id") String id) {
        return ResponseEntity.ok(devDataExportTaskService.getById(id));
    }

    /**
     * 新增总导出任务表
     *
     * @param devDataExportTask 总导出任务表
     * @return R
     */
    @ApiOperation(value = "新增总导出任务表", notes = "新增总导出任务表")
    @PostMapping
    @AnonymousAccess
    public ResponseEntity<Object> save(@RequestBody DevDataExportTask devDataExportTask) {
        devDataExportTaskService.createExportTask(devDataExportTask);
        return ResponseEntity.ok(true);
    }

    /**
     * 修改总导出任务表
     *
     * @param devDataExportTask 总导出任务表
     * @return R
     */
    @ApiOperation(value = "修改总导出任务表", notes = "修改总导出任务表")
    @PutMapping
    public ResponseEntity<Object> updateById(@RequestBody DevDataExportTask devDataExportTask) {
        return ResponseEntity.ok(devDataExportTaskService.updateById(devDataExportTask));
    }

    /**
     * 通过id删除总导出任务表
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除总导出任务表", notes = "通过id删除总导出任务表")
    @DeleteMapping("/{id}")
    public ResponseEntity<Object> removeById(@PathVariable String id) {
        devDataExportTaskService.deleteById(id);
        return ResponseEntity.ok("ok");
    }

    /**
     * 通过id批量删除总导出任务表
     *
     * @param ids ids
     * @return R
     */
    @ApiOperation(value = "通过id删除总导出任务表", notes = "通过id删除总导出任务表")
    @DeleteMapping("/batchRemove/{ids}")
    public ResponseEntity<Object> removeByIds(@PathVariable List<String> ids) {
        devDataExportTaskService.removeByIds(ids);
        return ResponseEntity.ok("ok");
    }

    @ApiOperation(value = "通过id删除FTP上的数据文件", notes = "通过id删除FTP上的数据文件")
    @DeleteMapping("/del/data/file/{id}")
    public ResponseEntity<Object> removeDataFileById(@PathVariable String id) {
        devDataExportTaskService.logicRemoveDataFileById(id);
        return ResponseEntity.ok("ok");
    }

    /**
     * 通过id删除FTP上的数据文件
     *
     * @param ids ids
     * @return R
     */
    @ApiOperation(value = "通过id删除FTP上的数据文件", notes = "通过id删除FTP上的数据文件")
    @DeleteMapping("/del/data/file/batchDel")
    public ResponseEntity<Object> batchRemoveDataFileById(@RequestBody List<String> ids) {
        devDataExportTaskService.batchLogicRemoveDataFileById(ids);
        return ResponseEntity.ok("ok");
    }

    @ApiOperation(value = "导入开发数据同步自定义清单", notes = "导入开发数据同步自定义清单")
    @PostMapping(value = "/upload")
    public ResponseEntity<Object> importFromExcel(@RequestParam("file") MultipartFile file) {
        return ResponseEntity.ok(devDataExportTaskService.importFromExcel(file));
    }

    @ApiOperation(value = "服务重启任务继续执行", notes = "服务重启任务继续执行")
    @PutMapping(value = "/continue/run/{id}")
    public ResponseEntity<Object> continueRunById(@PathVariable("id") String id) {
        log.info("服务重启任务继续执行：{}", id);
        devDataExportTaskService.continueRunById(id);
        return ResponseEntity.ok("ok");
    }

    @ApiOperation(value = "开发数据导出任务暂停", notes = "开发数据导出任务暂停")
    @PutMapping("/suspend")
    @AnonymousAccess
    public ResponseEntity<Object> suspend(@RequestBody @NotNull List<String> ids) {
        log.info("开发数据导出任务暂停：{}", StrUtil.join(",", ids));
        devDataExportTaskService.suspend(ids);
        return ResponseEntity.ok("ok");
    }

    @ApiOperation(value = "开发数据导出任务中止", notes = "开发数据导出任务中止")
    @PutMapping("/abolish")
    public ResponseEntity<Object> abolish(@RequestBody @NotNull List<String> ids) {
        log.info("开发数据导出任务中止：{}", StrUtil.join(",", ids));
        devDataExportTaskService.abolish(ids);
        return ResponseEntity.ok("ok");
    }

    @ApiOperation(value = "开发数据导出任务暂停后重启", notes = "开发数据导出任务暂停后重启")
    @PutMapping("/restart")
    @AnonymousAccess
    public ResponseEntity<Object> restart(@RequestBody @NotNull List<String> ids) {
        log.info("开发数据导出任务暂停后重启：{}", StrUtil.join(",", ids));
        devDataExportTaskService.restart(ids);
        return ResponseEntity.ok("ok");
    }
}
